package com.zzg.datastructure.huffmancode.entity;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Objects;

/**
 * @Author zhengzg
 * @Date 2022-09-29
 * @Version v1.0
 */
@Getter
@Setter
@ToString(of = {"data","weight"})
public class Node implements Comparable<Node>{

    /**
     * 存放数据
     */
    Byte data;

    /**
     * 权值，表示字符出现的次数
     */
    int weight;

    Node left;

    Node right;

    public Node(Byte data, int weight) {
        this.data = data;
        this.weight = weight;
    }

    @Override
    public int compareTo(Node o) {
        return this.weight - o.weight;
    }

    public void preOrder() {
        System.out.println(this);
        if (Objects.nonNull(this.left)) {
            this.left.preOrder();
        }
        if (Objects.nonNull(this.right)) {
            this.right.preOrder();
        }
    }

}
